//
//  KefuVC.m
//  SSC
//
//  Created by 红沙尘 on 17/6/8.
//  Copyright © 2017年 红沙尘. All rights reserved.
//

#import "KefuVC.h"

@interface KefuVC ()<UITextViewDelegate>
{
    __weak IBOutlet UITextField *_nameField;
    __weak IBOutlet UITextField *_emailField;
    __weak IBOutlet UITextView  *_textView;
    
}
@end

@implementation KefuVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if (self.type == KefuTypeKefu) {
        [self setTitle:@"联系客服"];
    } else if (self.type == KefuTypeMima) {
        [self setTitle:@"找回密码"];
    }
    
    
    [self setupUI];
}

- (void)setupUI {

    _textView.layer.cornerRadius    = 2;
    _textView.layer.masksToBounds   = YES;
    _textView.layer.borderWidth = LineHeight;
    _textView.layer.borderColor = COLOR_CCCCCC.CGColor;
    
    [self setupTextField:_nameField];
    [self setupTextField:_emailField];

    _textView.text = @"必填";
    [_textView setTextColor:ARGB(153, 153, 153, 1)];
}

- (void)setupTextField:(UITextField *)view {
    
    view.layer.cornerRadius   = 2;
    view.layer.masksToBounds  = YES;
    view.layer.borderWidth = LineHeight;
    view.layer.borderColor = COLOR_CCCCCC.CGColor;
    view.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 20)];
    view.leftViewMode = UITextFieldViewModeAlways;
    
}

#pragma mark - event

- (IBAction)tijiao {
    
    if (_nameField.text.length == 0) {
        [MTool showAlertController:self message:@"用户名称必须填写"];
        return;
    }
    
    if (_emailField.text.length == 0) {
        [MTool showAlertController:self message:@"邮箱地址必须填写"];
        return;
    }
    
    if (_textView.text.length == 0) {
        [MTool showAlertController:self message:@"留言内容必须填写"];
        return;
    }
    
    [LoadingView show];
    
    WeakSelf
    
    [SSCRequest userforgetpassWithUsername:_nameField.text
                                     email:_emailField.text
                                   content:_textView.text
                                   success:^(NSDictionary *responseObject)
    {
        [LoadingView diss];

        [MTool showMessage:responseObject[@"msg"] inView:self.view complete:^{
            [weakSelf.navigationController popToRootViewControllerAnimated:YES];
        }];
    } fail:^(NSError *error) {
        [LoadingView diss];
    }];
}

#pragma mark - UITextViewDelegate

- (void)textViewDidBeginEditing:(UITextView *)textView
{
    if ([textView.text isEqualToString:@"必填"])
    {
        textView.text = @"";
        [textView setTextColor:ARGB(51, 51, 51, 1)];
    }
    else if (textView.text.length < 1)
    {
        textView.text = @"必填";
        [textView setTextColor:ARGB(153, 153, 153, 1)];
    }
}

- (void)textViewDidEndEditing:(UITextView *)textView
{
    if (textView.text.length < 1)
    {
        textView.text = @"必填";
        [textView setTextColor:ARGB(153, 153, 153, 1)];
    }
}


@end
